;
(function() {
    new Vue({
        el: '#wx-config',
        data: {
            navLiData: [{ name: '模板配置', code: true }, { name: '项目配置', code: false }, { name: '关注列表', code: true }],
            navAct: 0,
            permissions: JSON.parse(window.localStorage.getItem('permissions')),
            wxId: hlUrlUtil.GetQueryString('id'),
            wxGZHdata: { type: null, img: '' },
            wxGZHtpData: [],
            wxGZHproData: [],
            pages: [10, 15, 20],
            pagesCur: 10,
            pageNum: 1,
            seaName: null,
            userProData: [],
            userProFlag: false,
            putUserData: [],
            checkAllFlag: false,
            WXfocusOnData: [],
            typeIO: null,
            SmWxFlag: false
        },
        filters: {
            timestamp: function(time) {
                if (!time) return '--';
                return hlDateUtil.formatUTCDate(time)
            },
            toDou: function(n) {
                return n < 10 ? '0' + n : '' + n;
            }
        },
        methods: {
            copyToClipboard(value){
					    var currentFocus = document.activeElement;// 保存当前活动节点
					    
					    var input = document.createElement('input');// 创建一个input标签
					    document.body.appendChild(input);// 把标签添加给body
					    input.style.opacity = 0;//设置input标签设置为透明(不可见)
					    input.value = value;// 把需要复制的值放到input上
					
					    // 记录当前滚动位置, 因为添加节点并选中的时候会影响页面滚动
					    var scrollY = window.scrollY;
					
					    input.focus();// input节点获取焦点
					    input.setSelectionRange(0, input.value.length);// 选中input框中的所有文字
					
					    var res = document.execCommand('copy', true);// 复制文字并获取结果
					
					    currentFocus.focus();// 之前活动节点获得焦点
					    document.body.removeChild(input);// 删除添加的input节点
					
					    // 页面滚动到之前位置
					    window.scrollTo(0, scrollY);
					    
					    return res;// 返回操作结果
					},
				
					copyOpenId (item) {
						if (!item.openId) return;
						console.log(item.openId)
						this.copyToClipboard(item.openId)
						hl.toast('复制成功')						

					},
            // 导航切换
            navButton: function(i) {
                this.navAct = i;
                var is = this;
                if (i == 1) {
                    //获取公众号项目列表
                    this.getProject()
                }
                if (i == 2) {
                    this.getFocusOnList()
                }
            },
            //获取项目列表
            getProject: function() {
                var is = this;
                hlhttp.get({
                    url: 'wx_pns/projects',
                    data: { wxpn_id: is.wxGZHdata.id },
                    success: function(res) {
                        is.wxGZHproData = res;
                    }
                })
            },
            //获取公众号信息
            getWxpns: function() {
                var is = this;
                hlhttp.get({
                    url: 'wx_pns/info',
                    data: { wxpn_id: is.wxId },
                    success: function(res) {
                        is.wxGZHdata = res;
                        if (res.type == 'I') {
                            is.typeIO = 'users';
                        }
                        if (res.type == 'O') {
                            is.typeIO = 'customers';
                        }
                        if (is.permissions.indexOf('c105') > -1) {
                            is.getWxTp(res.id)
                        }
                        if (!is.navLiData[1].code && !is.navLiData[0].code) {
                            is.getFocusOnList()
                        }
                    }
                })
            },
            //获取微信公众号消息模板列表
            getWxTp: function() {
                var is = this;

                hlhttp.get({
                    url: 'wxpn_message_templates',
                    data: { wxpn_id: is.wxGZHdata.id, channel: 'WX', type: this.wxGZHdata.type },
                    success: function(res) {
                        for (var i = 0; i < res.length; i++) {
                            res[i].flag = false;
                        }
                        is.wxGZHtpData = res;
                    }
                })
            },
            //保存微信公众号消息模板ID
            saveWxTp: function(f, i, id1, id2) {
                this.wxGZHtpData[i].flag = f;
                var is = this;
                if (f == 'false') {
                    hlhttp.put({
                        url: 'wxpn_message_templates',
                        data: {
                            template_id: id2 || '#',
                            wxpn_message_template_id: id1
                        },
                        success: function() {
                            hl.toast('微信消息模板ID修改成功')
                            is.wxGZHtpData[i].flag = false;
                        }
                    })
                }
            },
            //获取当前用户项目列表
            getUserPro: function() {
                var is = this;
                this.userProFlag = true;
                hlhttp.get({
                    url: 'tenant/fn_projects',
                    success: function(res) {

                        Array.prototype.distinct = function() {
                            var arr = this,
                                result = [],
                                i,
                                j,
                                len = arr.length;
                            for (i = 0; i < len; i++) {
                                for (j = i + 1; j < len; j++) {
                                    if (arr[i].projectId === arr[j].projectId) {
                                        j = ++i;
                                    }
                                }
                                result.push(arr[i]);
                            }
                            return result;
                        }

                        is.userProData = res.distinct();

                        var resArr = res.distinct()
                        for (var i = 0; i < resArr.length; i++) {
                            resArr[i].flag = false;
                            if (resArr[i].wxOid) {
                                if (is.wxGZHdata.type == 'O') {
                                    resArr[i].flag1 = true;
                                }
                                if (is.wxGZHdata.id == resArr[i].wxOid) {
                                    if (is.wxGZHdata.type == 'O') {
                                        is.putUserData.push(resArr[i])
                                    }
                                }
                            }
                            if (resArr[i].wxIid) {
                                if (is.wxGZHdata.type == 'I') {
                                    resArr[i].flag1 = true;
                                }
                                if (is.wxGZHdata.id == resArr[i].wxIid) {
                                    if (is.wxGZHdata.type == 'I') {
                                        is.putUserData.push(resArr[i])
                                    }
                                }
                            }
                        }

                    }
                })
            },
            //全选项目   灰色不能选
            checkAllPro: function() {
                if (this.checkAllFlag) {
                    this.checkAllFlag = false;
                } else {
                    this.checkAllFlag = true;
                }
                this.putUserData = [];
                for (var i = 0; i < this.userProData.length; i++) {
                    var fff = false;
                    var p = this.userProData[i];
                    if (p.flag1) {
                        this.putUserData.splice(i, 1)
                    };

                    if (this.checkAllFlag) {
                        p.flag = true;
                        if (this.wxGZHdata.id == p.wxIid) {
                            if (this.wxGZHdata.type == 'I') {
                                fff = true;
                            }
                        }
                        if (!p.wxIid) {
                            if (this.wxGZHdata.type == 'I') {
                                fff = true;
                            }
                        }
                        if (this.wxGZHdata.id == p.wxOid) {
                            if (this.wxGZHdata.type == 'O') {
                                fff = true;
                            }
                        }
                        if (!p.wxOid) {
                            if (this.wxGZHdata.type == 'O') {
                                fff = true;
                            }
                        }
                        if (fff) this.putUserData.push(this.userProData[i]);
                    } else {
                        p.flag = false;
                        if (this.userProData[i].flag1) {
                            if (this.wxGZHdata.id == p.wxIid) {
                                if (this.wxGZHdata.type == 'I') {
                                    this.putUserData.push(this.userProData[i])
                                }
                            }
                            if (this.wxGZHdata.id == p.wxOid) {
                                if (this.wxGZHdata.type == 'O') {
                                    this.putUserData.push(this.userProData[i])
                                }
                            }
                        };
                    }
                }
            },
            //单选删除  灰色不能选
            checkPro: function(id, j) {
                for (var i = 0; i < this.userProData.length; i++) {
                    var p = this.userProData[i];
                    if (p.projectId == id) {
                        p.flag = false;
                        p.flag1 = false;
                        this.putUserData.splice(j, 1)
                        this.checkAllFlag = false;
                    }
                }
            },
            //单选选中
            checkRadio: function(item, i) {
                if (!this.userProData[i].flag) {
                    this.userProData[i].flag = true;
                    this.putUserData.push(item)
                    return
                }
                if (this.userProData[i].flag) {
                    this.userProData[i].flag = false;
                    for (var j = 0; j < this.putUserData.length; j++) {
                        if (this.putUserData[j].projectId == item.projectId) {
                            this.putUserData.splice(j, 1)
                        }
                    }
                }
            },
            // 关闭取消
            closePro: function() {
                this.checkAllFlag = false;
                this.userProFlag = false;
                this.putUserData = [];
                for (var i = 0; i < this.userProData.length; i++) {
                    this.userProData[i].flag = false
                }
            },
            //配置项目公众号
            configPro: function() {
                var is = this;
                var ids = [];
                for (var i = 0; i < this.putUserData.length; i++) {
                    ids.push(this.putUserData[i].projectId)
                }
                hlhttp.put({
                    url: 'projects/wxpn',
                    data: {
                        wxpn_id: is.wxGZHdata.id,
                        opt: is.wxGZHdata.type,
                        project_ids: ids.toString() || '#'
                    },
                    success: function() {
                        hl.toast('保存成功')
                        is.checkAllFlag = false;
                        is.userProFlag = false;
                        is.putUserData = [];
                        is.getProject()
                    }
                })
            },
            //获取微信开发平台用户授权链接
            getSMWx: function(id, name) {
                var is = this;
                this.SmWxFlag = true;
                hlhttp.get({
                    url: 'wxpn_auth_url',
                    data: {
                        user_id: id,
                        wxpn_id: is.wxGZHdata.id,
                        user_type: is.wxGZHdata.type == 'I' ? 'U' : 'C',
                        user_name: name
                    },
                    success: function(res) {
                        qrcode.makeCode(res.wxpnAuthUrl);
                    }
                })
            },
            //关闭的时候调下接口
            closeWXewm: function() {
                this.SmWxFlag = false;
                this.getFocusOnList()
            },
            //获取公众号关注用户列表
            getWxFocus: function(data) {
                var is = this;
                hlhttp.get({
                    url: 'wxpns/wxpn_users',
                    data: { wxpn_id: is.wxGZHdata.id },
                    success: function(res) {
                        //对内
                        if (is.wxGZHdata.type == 'I') {
                            for (var j = 0; j < data.users.length; j++) {
                                data.users[j].wxNickName = '';
                                data.users[j].openId = '';
                                for (var i = 0; i < res.length; i++) {
                                    if (res[i].userId == data.users[j].id) {
                                        data.users[j].wxNickName = res[i].wxNickName
                                        data.users[j].openId = res[i].openId
                                        data.users[j].bindId = res[i].id
                                    }
                                }
                            }
                            is.WXfocusOnData = data;
                        }
                        //对外
                        if (is.wxGZHdata.type == 'O') {
                            for (var j = 0; j < data.customers.length; j++) {
                                data.customers[j].wxNickName = '';
                                data.customers[j].openId = '';
                                for (var i = 0; i < res.length; i++) {
                                    if (res[i].userId == data.customers[j].id) {
                                        data.customers[j].wxNickName = res[i].wxNickName
                                        data.customers[j].openId = res[i].openId
																				//bind
																				data.customers[j].bindId = res[i].id
                                    }
                                }
                            }
                            is.WXfocusOnData = data;
                        }
                    }
                })
            },
            //获取关注列表
            getFocusOnList: function() {
                var is = this;
                //对内
                if (this.wxGZHdata.type == 'I') {
                    hlhttp.get({
                        url: 'tenants/users/_search_create_time',
                        data: {
                            page: is.pageNum,
                            limit: is.pagesCur,
                            beginTime: '#',
                            endTime: '#',
                            key_word: is.seaName || '#',
                            source: 'S',
                        },
                        success: function(res) {
                            is.getWxFocus(res)
                            $("#page").initPage(res.totalPage, is.pageNum, function(n) {
                                if (is.pageNum == n) {
                                    return
                                }
                                is.pageNum = n;
                                is.getFocusOnList();
                            });
                        }
                    })
                }
                //对外
                if (this.wxGZHdata.type == 'O') {
                    hlhttp.get({
                        url: 'customers/_search',
                        data: {
                            begin_date: '#',
                            end_date: '#',
                            key_word: is.seaName || '#',
                            rperson_ids: '#',
                            page: is.pageNum,
                            page_size: is.pagesCur,
                            customer_state: '#',
                            black_state: '#',
                            columns: 'RT',
                            orders: 'DESC'
                        },
                        success: function(res) {
                            // is.WXfocusOnData = res;
                            is.getWxFocus(res)
                            $("#page").initPage(res.totalPage, is.pageNum, function(n) {
                                if (is.pageNum == n) {
                                    return
                                }
                                is.pageNum = n;
                                is.getFocusOnList();
                            });
                        }
                    })
                }
            },
            //重置
            resetWXfocus: function() {
                this.seaName = null;
                this.pageNum = 1;
                this.pagesCur = 10;
                $('.pagenums span').text('10')
                this.getFocusOnList()
            },
            //每页显示数量
            limitPage: function(n) {
                this.pagesCur = n;
                this.getFocusOnList()
            },
            //解除绑定公众号关注用户

            JBwx: function(item) {
							console.log(item);
							
                var is = this;
                hlhttp.delete({
                    url: 'wxpn_users?open_id=' + item.openId+'&id='+item.bindId,
                    success: function() {
                        hl.toast('解除绑定成功')
                        is.getFocusOnList()
                    }
                })
            }
        },
        mounted: function() {
            this.getWxpns()
            hlSelectUi(true)
            if (this.permissions.indexOf('c101') > -1) this.navLiData[1].code = true;
        }
    })

    var qrcode = new QRCode(document.getElementById("qrcode"), {
        width: 180,
        height: 180
    });

}())
